import { CommaSeparatedListOutputParser } from "@langchain/core/output_parsers";
import { ChatOllama } from "@langchain/ollama";
import { PromptTemplate } from "@langchain/core/prompts";

const pt = PromptTemplate.fromTemplate(
  "{instructions} \n 列出3个 {country} 的著名的互联网公司."
);
/**
 * 完整的提示词：
 * Your response should be a list of comma separated values, eg: `foo, bar, baz`
 * 列出3个 中国 的著名的互联网公司.
 */

const model = new ChatOllama({
  model: "llama3",
  temperature: 0.7,
});

const parser = new CommaSeparatedListOutputParser();

const chain = pt.pipe(model);

const res = await chain.stream({
  country: "中国",
  instructions: parser.getFormatInstructions(),
});

console.log("解析器解析前：");
let output = ""; // 拼接完整的结果
for await (const chunk of res) {
  process.stdout.write(chunk.content);
  output += chunk.content;
}

const result = await parser.invoke(output);

console.log("\n\n解析器解析后：", result);
